package com.example.modules.user;

import cn.hutool.json.JSONObject;
import com.example.generator.model.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @GetMapping
    public List<User> userList() {
        return userService.userList();
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public void register(@RequestBody JSONObject param) {
        userService.register(param);
    }

    /**
     * 修改个人信息
     */
    @PostMapping("/modifyInfo")
    @PreAuthorize("hasAuthority('updatePersonalDetail')")
    public UserDetailsVO modifyInfo(@RequestBody UserDetailsVO user, @AuthenticationPrincipal UserDetailsVO userInfo) {
        return userService.modifyInfo(userInfo.getId(), user);
    }

    /**
     * 忘记密码，根据用户信息找回密码
     */
    @PostMapping("/forget")
    public void forget(@RequestBody JSONObject jsonObject) {
        userService.forget(jsonObject);
    }

    /**
     * 修改当前用户密码
     */
    @PostMapping("/changePassword")
    public void changePassword(@RequestBody JSONObject jsonObject, @AuthenticationPrincipal UserDetailsVO userInfo) {
        userService.changePassword(jsonObject, userInfo);
    }
}
